<?php

class NewsApp extends MemberbaseApp
{
	var $_news_mod;
	var $_cate_mod;
	var $_user;
	var $_user_mod;
    function __construct()
    {
        $this->NewsApp();
    }
    function NewsApp()
    {
        parent::__construct();
		$this->_news_mod =& m('news');
		$this->_cate_mod =& m('category');
		$this->_user = $this->visitor->get();
		$this->_user_mod =& m('member');
		$this->assign('this_time', gmtime());
    }
	
    function index()
    {
		$cate_id = empty($_GET['cate_id']) ? 0 : intval($_GET['cate_id']);
		if ($cate_id) {
			$conditions = 'FIND_IN_SET('.$cate_id.',news.cate_id)';
		} 
		else
		{
			$conditions = '1=1';
		}

		if($this->_user['role']==USER_FACTORY){
			$news_cate=NEWS_CATE_TZ_GC;
		}elseif ($this->_user['role']==USER_CLIENT){
			$news_cate=NEWS_CATE_TZ_KH;
		}elseif (in_array($this->_user['role'], array(USER_MANAGER,USER_MERCHANDISER_ADMIN,USER_OUTGOING,USER_FINANCE))){
			$this->assign('user_role',true);
		}elseif ($this->_user['role']==USER_MERCHANT){
			$news_cate=NEWS_CATE_TZ_MERCH;
		}else{
			$news_cate=NEWS_CATE_TZ_YG;
		}
		//限定只显示通知和折扣
		if (!$cate_id){
			if ($news_cate){
				$conditions .= ' AND ((FIND_IN_SET('.$news_cate.',news.cate_id) AND news.if_show = 1) or news.from_id='.$this->visitor->get("user_id").')';
			}else{
				$conditions .= ' AND ((news.if_show = 1) or news.from_id='.$this->visitor->get("user_id").')';
			}
		}
		
		$this->assign('user_id', $this->visitor->get("user_id"));
		
		$page = $this->_get_page();
		$news_list = $this->_news_mod->find(array(
			'conditions' => $conditions,
			'fields' => '*, news.if_show',
			//'join' => 'belongs_cate',
			'order' => 'update_date DESC',
			'count' => true,
			'limit' => $page['limit']
		));
		if (!empty($news_list)){
			foreach ($news_list as $key=>$value){
				$cate_arr_id=explode(',', $value['cate_id']);
				$news_list[$key]['cate_arr_id']=$cate_arr_id;
			}
		}
		$page['item_count'] = $this->_news_mod->getCount();
		$this->_format_page($page);
		$this->assign('user', $this->visitor->get());
		$this->assign('page_info', $page);
		$this->assign('news_list', $news_list);
		$cate_list = $this->_get_cate_list();
        $this->display('news.index.html');
        /* 当前位置 */
		$cate=LANG::get("news_cate");
		if ($cate_id) {
			
        	$this->_curlocal(LANG::get('history_news'), 'index.php?app=news', $cate[$cate_id]);
		} else {
        	$this->_curlocal(LANG::get('history_news'));
		}
    }
	
	function add()
	{
		if (!IS_POST) {
			$this->_assign_form();
			$this->_get_cate_list();
			/* 加载编辑器 */
            $this->assign('build_editor', $this->_build_editor(array(
                'name' => 'content',
                //'content_css' => SITE_URL . "/themes/store/{$template_name}/styles/{$style_name}" . '/shop.css', // 预览使用
            )));
			//赋值接收人用户信息
			$user_gc=$this->_user_mod->find("is_factory=1 AND user_id !={$this->visitor->get('user_id')}");
			$user_yg=$this->_user_mod->find("is_factory=0 AND user_id !={$this->visitor->get('user_id')}");
			$this->assign('user_gc', $user_gc);
			$this->assign('user_yg', $user_yg);
			$this->assign('default_new_id',DEFAULT_CUSTOMER_KEY_ID);
			$this->assign('belongs_news', BELONGS_NEWS);
			$this->assign('images', $this->_get_images());
	        $this->_config_seo('title', Lang::get('add_news'));
	        $this->display('news.form.html');
        	/* 当前位置 */
        	$this->_curlocal(LANG::get('add_news'));
		} else {
			$title = trim($_POST['title']);
			$cate_id =$_POST['cate_id'];
			$cate_arr_id=implode(',', $cate_id);
			$content = trim($_POST['content']);
			$if_show = intval($_POST['if_show']);
			$update_date = time();
//			$recipient = $_POST['recipient'];
//			if(($cate_id==NEWS_CATE_TZ||$cate_id==NEWS_CATE_ZK)&&empty($recipient))
//			{
//				$this->show_warning('recipient_required');
//				return;
//			}

			if (empty($title)) {
				$this->show_warning('title_not_empty');
				return;
			}
			if (!$this->_news_mod->unique($title)) {
				$this->show_warning('该信息标题重复');
				return;
			}
			if (!$cate_arr_id) {
				$this->show_warning('category_select_pls');
				return;
			}
			if (empty($content)) {
				$this->show_warning('content_not_empty');
				return;
			}
			
			$data = array(
				'title' => $title,
				'cate_id' => $cate_arr_id,
				'content' => $content,
				'if_show' => $if_show,
				'update_date' => $update_date,
				'from_id'	=>$this->visitor->get('user_id')
			);
//			if($cate_id==NEWS_CATE_GC||$cate_id==NEWS_CATE_YG)
//			{
				$news_id = $this->_news_mod->add($data);
//			}
//			else
//			{
//				foreach($recipient as $item)
//				{
//					$data["to_id"]=$item;
//					$news_id=$this->_news_mod->add($data);
//				}
//			}
			if (!$news_id) {
				$this->_error($this->_news_mod->get_error());
				return;
			}
			/* 图片入库 */
			if ($_POST['images']) {
				$this->_save_images($_POST['images'], $news_id);
			}
			$this->show_message('add_news_success', Lang::get('history_news'), 'index.php?app=news');
		}
	}
	
	function _assign_form()
	{
		$news = array(
			'if_show' => 1,
		);
		$this->assign('news', $news);
	}
	
	function edit()
	{
		
		$news_id = intval($_GET['news_id']);
		if (!$news_id) {
			$this->show_warning('news_not_exit');
			return;
		}
		$news = $this->_news_mod->get_info($news_id);
		if(!$news || $news["from_id"]!=$this->visitor->get('user_id')) {
			$this->show_warning('news_not_exit');
			return;
		}
		
		if (!IS_POST) {
			if (!empty($news['cate_id'])){
				$c_arr_id=explode(',', $news['cate_id']);
				$news['c_arr_id']=$c_arr_id;
			}
			$this->assign('news', $news);
			$this->_get_cate_list();
			/* 加载编辑器 */
            $this->assign('build_editor', $this->_build_editor(array(
                'name' => 'content',
            )));
			
			/* 获取附件 */
			$file_mod =& m('upload');
			$files = $file_mod->find(array(
				'conditions' => 'file_type = ' . BELONGS_NEWSFILE . ' AND key_id = ' . $news_id,
			));
			$this->assign('files', $files);
			
			//赋值接收人用户信息
			$user_gc=$this->_user_mod->find("is_factory=1 AND user_id !={$this->visitor->get('user_id')}");
			$user_yg=$this->_user_mod->find("is_factory=0 AND user_id !={$this->visitor->get('user_id')}");
			$this->assign('user_gc', $user_gc);
			$this->assign('user_yg', $user_yg);
			
			$this->assign('belongs_news', BELONGS_NEWS);
			$this->assign('belongs_newsfile', BELONGS_NEWSFILE);
			$this->assign('images', $this->_get_images($news_id));
	        $this->display('news.form.html');
        	/* 当前位置 */
        	$this->_curlocal(LANG::get('edit_news'));
		} else {
			$title = trim($_POST['title']);
			$cate_id = $_POST['cate_id'];
			$cate_arr_id=implode(',', $cate_id);
		//	$to_id = intval($_POST['recipient']);
			$content = trim($_POST['content']);
			$if_show = intval($_POST['if_show']);
			$update_date = time();
			
			if (empty($title)) {
				$this->show_warning('title_not_empty');
				return;
			}
			if (!$this->_news_mod->unique($title, $news_id)) {
				$this->show_warning('该信息标题重复');
				return;
			}
			if (!$cate_arr_id) {
				$this->show_warning('category_select_pls');
				return;
			}
			if (empty($content)) {
				$this->show_warning('content_not_empty');
				return;
			}
			
			$data = array(
				'title' => $title,
				'cate_id' => $cate_arr_id,
				'content' => $content,
				'if_show' => $if_show,
				'update_date' => $update_date,
				'from_id'	=>$this->visitor->get('user_id'),
	//			'to_id'=>$to_id
			);
			
			$this->_news_mod->edit($news_id, $data);
			if ($this->_news_mod->get_error()) {
				$this->_error($this->_news_mod->get_error());
				return;
			}
			/* 图片入库 */
			if ($_POST['images']) {
				$this->_save_images($_POST['images'], $news_id);
			}
			$this->show_message('edit_news_success', Lang::get('history_news'), 'index.php?app=news');
		}
	}
	
	function del()
	{
		
		$news_id = intval($_GET['news_id']);
		if (!$news_id) {
			$this->show_warning('news_not_exit');
			return;
		}
		$news = $this->_news_mod->get_info($news_id);
		if(!$news || $news["from_id"]!=$this->visitor->get('user_id')) {
			$this->show_warning('news_not_exit');
			return;
		}
		
			
			$this->_news_mod->drop("news_id={$news_id}");
			if ($this->_news_mod->get_error()) {
				$this->_error($this->_news_mod->get_error());
				return;
			}
			$this->show_message('删除消息成功', Lang::get('history_news'), 'index.php?app=news');

	}
	
	function _get_images($id=DEFAULT_CUSTOMER_KEY_ID)
	{
		$file_mod =& m('upload');
		$images = $file_mod->find(array(
			'conditions' => 'file_type = ' . BELONGS_NEWS . ' AND key_id = '.$id,
			'order' => 'file_id ASC',
		));
		return $images;
	}
	
	function _save_images($images, $key_id)
	{
		$file_mod =& m('upload');
		foreach ($images as $id) {
			$file_mod->edit($id, array('key_id'=>$key_id));
		}
	}
	
	function cate()
	{
		
		if (!$this->_user_mod->check_powers($this->_user['user_id'], array(USER_ADMIN, USER_INFO))) {
			$this->show_warning('no_powers_to_visit');
			return;
		};
		
		$cate_list = $this->_cate_mod->findAll();
		$this->assign('cate_list', $cate_list);

        $this->display('category.index.html');
        /* 当前位置 */
        $this->_curlocal(LANG::get('category_manage'));
	}
	
	function _assign_cate_form()
	{
		$category = array(
			'if_show' => 1
		);
		$this->assign('category', $category);
	}
	
	function cate_add()
	{
		if (!$this->_user_mod->check_powers($this->_user['user_id'], array(USER_ADMIN, USER_INFO))) {
			$this->show_warning('no_powers_to_visit');
			return;
		};
		
		if (!IS_POST) {
			$this->_assign_cate_form();
			
			$this->display('category.form.html');
        	/* 当前位置 */
        	$this->_curlocal(LANG::get('add_category'));
		} else {
			$cate_name = trim($_POST['cate_name']);
			$if_show = intval($_POST['if_show']);
			
			if (empty($cate_name)) {
				$this->show_warning('cate_name_not_empty');
				return;
			}
			if (!$this->_cate_mod->unique($cate_name)) {
				$this->show_warning('cate_name_is_exist');
				return;
			}
			
			$data = array(
				'cate_name' => $cate_name,
				'if_show' => $if_show
			);
			
			$cate_id = $this->_cate_mod->add($data);
			if (!$cate_id) {
				$this->_error($this->_cate_mod->get_error());
				return;
			}
			$this->show_message('add_category_success', Lang::get('category_manage'), 'index.php?app=news&act=cate');
		}
	}
	
	function cate_edit()
	{
		if (!$this->_user_mod->check_powers($this->_user['user_id'], array(USER_ADMIN, USER_INFO))) {
			$this->show_warning('no_powers_to_visit');
			return;
		};
		
		$cate_id = intval($_GET['cate_id']);
		if (!$cate_id) {
			$this->show_warning('cate_not_exist');
			return;
		}
		$category = $this->_cate_mod->get_info($cate_id);
		if (!$category) {
			$this->show_warning('cate_not_exist');
			return;
		}
		
		if (!IS_POST) {
			$this->assign('category', $category);
			$this->display('category.form.html');
        	/* 当前位置 */
        	$this->_curlocal(LANG::get('edit_category'));
		} else {
			$cate_name = trim($_POST['cate_name']);
			$if_show = intval($_POST['if_show']);
			
			if (empty($cate_name)) {
				$this->show_warning('cate_name_not_empty');
				return;
			}
			if (!$this->_cate_mod->unique($cate_name, $cate_id)) {
				$this->show_warning('cate_name_is_exist');
				return;
			}
			
			$data = array(
				'cate_name' => $cate_name,
				'if_show' => $if_show
			);
			
			$this->_cate_mod->edit($cate_id, $data);
			if ($this->_cate_mod->get_error()) {
				$this->_error($this->_cate_mod->get_error());
				return;
			}
			$this->show_message('edit_category_success', Lang::get('category_manage'), 'index.php?app=news&act=cate');
		}
	}
	
	function _get_cate_list()
	{
		$cate_list = $this->_cate_mod->find(array(
			'conditions' => 'if_show = 1',
		));
		$this->assign('cate_list', $cate_list);
		return $cate_list;
	}
	
	function show()
	{
		$id = empty($_GET['id']) ? 0 : intval($_GET['id']);
		if (!$id) {
			$this->show_warning('news_not_exist');
			return;
		}
		
		$news = $this->_news_mod->get(array(
			'conditions' => 'news_id = ' . $id . ' AND news.if_show = 1',
			//'join' => 'belongs_cate',
		));
		if (!$news) {
			$this->show_warning('news_not_exist');
			return;
		}
			if ($news['cate_id']){
				$news['cate_arr_id']=explode(',', $news['cate_id']);
			}
			/* 获取附件 */
			$file_mod =& m('upload');
			$files = $file_mod->find(array(
				'conditions' => 'file_type = ' . BELONGS_NEWSFILE . ' AND key_id = ' . $id,
			));
			$this->assign('files', $files);
		
		$this->assign('news', $news);
		$this->display('news.show.html');
		$cate=LANG::get("news_cate");
		if($this->_user['role']==USER_FACTORY){
			$news_cate=NEWS_CATE_TZ_GC;
		}elseif ($this->_user['role']==USER_CLIENT){
			$news_cate=NEWS_CATE_TZ_KH;
		}elseif ($this->_user['role']==USER_MERCHANT){
			$news_cate=NEWS_CATE_TZ_MERCH;
		}else{
			$news_cate=NEWS_CATE_TZ_YG;
		}
        /* 当前位置 */
		if($news['cate_id']==NEWS_CATE_GC)
		{
			$this->_curlocal("帮助中心", 'index.php?app=news&act=helpcenter', 
						$news['title']);	
		}
		elseif($news['cate_id']==NEWS_CATE_YG)
        {
			$this->_curlocal("培训平台", 'index.php?app=news&act=trainplat',
						$news['title']);	
		}
		elseif($news['cate_id']==NEWS_CATE_CLENT_HELP)
        {
			$this->_curlocal("系统培训", 'index.php?app=news&act=systrain',
						$news['title']);	
		}
		else
        {
			$this->_curlocal(LANG::get("history_news"), 'index.php?app=news', $cate[$news_cate],
						'index.php?app=news&cate_id=' . $news_cate,
						$news['title']);	
		}
	}
	
	//帮助中心
	function helpcenter()
	{
		//判断是否工厂用户
		//$is_factory=$this->visitor->get("is_factory");

		$conditions = 'FIND_IN_SET('.NEWS_CATE_GC.',news.cate_id) AND news.if_show=1 AND from_id !='.$this->visitor->get("user_id");
		
		$page = $this->_get_page();
		$news_list = $this->_news_mod->find(array(
			'conditions' => $conditions,
			'fields' => '*, news.if_show',
			//'join' => 'belongs_cate',
			'order' => 'update_date DESC',
			'count' => true,
			'limit' => $page['limit']
		));
		if (!empty($news_list)){
			foreach ($news_list as $key=>$value){
				$cate_arr_id=explode(',', $value['cate_id']);
				$news_list[$key]['cate_arr_id']=$cate_arr_id;
			}
		}
		$page['item_count'] = $this->_news_mod->getCount();
		$this->_format_page($page);
		$this->assign('page_info', $page);
		$this->assign('news_list', $news_list);
		$cate_list = $this->_get_cate_list();
        $this->display('news.helpcenter.html');
        /* 当前位置 */
        	$this->_curlocal(LANG::get('helpcenter'));
	}
	
	//培训平台
	function trainplat()
	{
		//判断是否工厂用户
		//$is_factory=$this->visitor->get("is_factory");

		$conditions = 'FIND_IN_SET('.NEWS_CATE_YG.',news.cate_id) AND news.if_show=1 AND from_id !='.$this->visitor->get("user_id");
		
		$page = $this->_get_page();
		$news_list = $this->_news_mod->find(array(
			'conditions' => $conditions,
			'fields' => '*, news.if_show',
			//'join' => 'belongs_cate',
			'order' => 'update_date DESC',
			'count' => true,
			'limit' => $page['limit']
		));
		if (!empty($news_list)){
			foreach ($news_list as $key=>$value){
				$cate_arr_id=explode(',', $value['cate_id']);
				$news_list[$key]['cate_arr_id']=$cate_arr_id;
			}
		}
		$page['item_count'] = $this->_news_mod->getCount();
		$this->_format_page($page);
		$this->assign('page_info', $page);
		$this->assign('news_list', $news_list);
		$cate_list = $this->_get_cate_list();
        $this->display('news.trainplat.html');
        /* 当前位置 */
        	$this->_curlocal(LANG::get('trainplat'));
	}
	
	
	//系统培训
	function systrain()
	{
		$conditions = 'FIND_IN_SET('.NEWS_CATE_CLENT_HELP.',news.cate_id) AND news.if_show=1 AND from_id !='.$this->visitor->get("user_id");
	
		$page = $this->_get_page();
		$news_list = $this->_news_mod->find(array(
				'conditions' => $conditions,
				'fields' => '*, news.if_show',
				'order' => 'update_date DESC',
				'count' => true,
				'limit' => $page['limit']
		));
		if (!empty($news_list)){
			foreach ($news_list as $key=>$value){
				$cate_arr_id=explode(',', $value['cate_id']);
				$news_list[$key]['cate_arr_id']=$cate_arr_id;
			}
		}
		$page['item_count'] = $this->_news_mod->getCount();
		$this->_format_page($page);
		$this->assign('page_info', $page);
		$this->assign('news_list', $news_list);
		$cate_list = $this->_get_cate_list();
		$this->display('news.trainplat.html');
		/* 当前位置 */
		$this->_curlocal(LANG::get('systrain'));
	}
}

?>
